02. Variable Analogies
Developing mental models or analogies is extremely useful when learning new concepts. This article on Teaching Variables: Analogies and Approaches gives some great visual analogies for variables, and you should really take a moment to read it!
Each balloon (variable) is accessible by its string (name).
Using Playgrounds
To start using variables in Swift, we are going to use Xcode Playgrounds. Playgrounds are an awesome part of Xcode that allow us to write and experiment with Swift code without having to create an entire application.
To create a Playground, open Xcode, and select “Get started with a Playground.” Alternatively, you can use the main toolbar and select File → New → Playground.
You can create a Playground from Xcode's launch screen.
You can give the Playground file any name you want, but keep the platform set to iOS. A good name might be "variables-practice".
Once the Playground file is created, you should see the following lines of code:
A new Playground contains a few lines of code.
By default, a new Playground includes a statement on the first line that reads //: Playground - noun: a place where people can play. This statement is what is known as a comment. A comment is some text used for documenting and describing code. Let's add some more comments now.
Adding Comments
There are two kinds of comments that we can add to our code: a single-line comment and a multi-line comment.
- To add a single-line comment, begin the line with two slashes. For example,
// This is a single-line comment. - To add a multi-line comment, begin the comment with the
/*characters. Then close the multi-line comment by finishing with the*/characters. For example:/* This is a really long multi-line comment that repeats itself so that it can fit across multiple lines. This is a really long multi-line comment that repeats itself so that it can fit across multiple lines. */
In this example, we add a single-line and multi-line comment.
Note: You can also add single-line comments after any valid statement. See the example below:
var str = "Hello, playground" // this statement creates a variable
If you cannot see the line numbers in your Playground file, then follow the instructions given in this Stack Overflow post.
Comments as Markup
You’ll notice that some comments contain a colon after the double slashes while others do not.
//: This comment has a colon after the slashes
// This comment does not have a colon after the slashes
The colon is a special character that tells your comments to use Markup. Markup is a formatting language that has been added to Playgrounds to make our comments more visually appealing. To enable the markdown formatting, go to Editor → Show Rendered Markup in the main toolbar.
Showing rendered Markup applies styling to the designated comments.
This will tell the comments that use colons to render using Markup. With Markup enabled, the comments with colons are set-off in nice frames whereas comments without colons just look like normal comments.
There is much more you can do with Markup—like adding headings, adding images, and styling text. If you're interested in learning more, check out Apple’s Markup reference. To disable the Markup rendering go back to Editor → Show Raw Markup in the main toolbar.
Now try adding some comments:
- Add a new single-line comment to the Playground.
- Add a new multi-line comment to the Playground.
- Add a new single-line comment to the Playground that uses Markup.
- Add a new multi-line comment to the Playground that uses Markup.
Note: If Markup is enabled and you try to create new comments using Markup, then they will not displayed until you disable and re-enable Markup.
The Remaining Statements
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
Getting back to the original statements, you might notice the statement import UIKit. We will talk more about this kind of statement later. For now, you can either remove or ignore it.
The last statement var str = “Hello, playground” creates a variable with the value “Hello, playground”. You can see in the pane on the right that the Playground gives us some textual output about the value used in this statement. Note: You can drag the right pane in and out if you need more space.